package iscript.uiFramework.components 
{
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import iscript.uiFramework.components.core.Background;
	import iscript.uiFramework.components.core.Border;
	import iscript.uiFramework.components.core.IBackground;
	import iscript.uiFramework.components.core.IBorder;
	import iscript.uiFramework.components.core.IDispose;
	import iscript.uiFramework.components.core.IRender;
	import iscript.uiFramework.effect.Effect;
	import iscript.uiFramework.event.UIComponentEvent;
	import iscript.uiFramework.UIFramework;
	import iscript.uiFramework.util.layout.LayoutManager;
	import iscript.uiFramework.util.LineSegment;
	import iscript.uiFramework.util.UIComponentUtil;
	
	
	
	/**
	 * UIComponent 最原始的类
	 */	
	public class UIComponent extends  Sprite implements IDispose,IRender
	{
		
		
	[Event(name="updateview", type = "iscript.uiFramework.event.UIComponentEvent")]
	[Event(name="resize",type="iscript.uiFramework.event.UIComponentEvent")]
	 /**
	  * Focus 
	  */
	[Event(name="focus_in",type="iscript.uiFramework.event.UIComponentEvent")]
	[Event(name="focus_out",type="iscript.uiFramework.event.UIComponentEvent")]
		 
		private var _id:String;
		private var _disposed:Boolean;		
		private var _border:Boolean;
		private var _borderEntity:Border;
		private var _disabled:Boolean;
		private var _enabled:Boolean;		
		private var _IsRender:Boolean;
		protected var layoutManager:LayoutManager;
		private var _initialized:Boolean;
		private var _focus:Boolean;
		protected  var _theme:String = "DefaultTheme";
		private var _background:Boolean;
		private var _backgroundEntity:Background;
		private var _IsRemoved:Boolean;
		
		
		protected var _preferWidth:Number;
		
		protected var _preferHeight:Number;
		
		/**
		 * 构造函数中初始化数据
		 */
		public function UIComponent(preferWidth:Number=0,preferHeight:Number=0) 
		{
			 _preferWidth = preferWidth;
			 _preferHeight = preferHeight;
			 _enabled = true;
			 layoutManager = new LayoutManager(this);
			 initialize();
			 eventListener();
			 this.addEventListener(Event.REMOVED_FROM_STAGE, _removeStageLogic);
			 this.addEventListener(Event.ADDED_TO_STAGE, _addToStageLogic);			
		}

		/* ====================================================================================
		 * Override
		 * ====================================================================================
		 */
/*		 override public function addChild(child:DisplayObject):flash.display.DisplayObject 
		{
			if(_rootContainer.parent)
			  return _rootContainer.addChild(child);
			
			return super.addChild(child);
		}
		
		override public function removeChild(child:DisplayObject):flash.display.DisplayObject 
		{
			if(_rootContainer.parent)
			 return _rootContainer.removeChild(child);
			 return super.removeChild(child);
		}
		override public function removeChildAt(index:int):flash.display.DisplayObject 
		{
			if(_rootContainer.parent)
			return _rootContainer.removeChildAt(index);
			return super.removeChildAt(index);
		}
		
		override public function removeChildren(beginIndex:int = 0, endIndex:int = 2147483647):void 
		{
			if(_rootContainer.parent)
			{
				_rootContainer.removeChildren(beginIndex, endIndex);
			return;
				}
				super.removeChildren(beginIndex, endIndex);
			}
		} */
		
		/* ====================================================================================
		 * Properties
		 * ====================================================================================
		 */
		
		 /**
		  * 可以交互状态，激活状态
		  */
		protected function get isActive():Boolean {
			return enabled && !disabled
			}
		 
		public function get theme():String 
		{
			if(_theme==null) _theme ="DefaultTheme"
			return _theme;
		}
		
		public function set theme(value:String):void 
		{
			_theme = value;
			updateRender();
		}
		
		/**
		 * 启用
		 */
		public function set  enabled(value:Boolean):void { _enabled = value; }
		/**
		 * 启用
		 */
		public function get  enabled():Boolean { return _enabled; }
		
		/**
		 * 灰色效果
		 */
		public function set  disabled(value:Boolean):void {
										  _disabled = value; 
										  if (value) {
											     Effect.gray(this);
											  }else {
												 Effect.reset(this);
											  }
										  updateRender();							
										}
		public function get  disabled():Boolean { return _disabled;}
		
		public function get Disposed():Boolean 
		{
			return _disposed;
		}
		
		override public function set width(value:Number):void 
		{
			 if (_borderEntity)_borderEntity.width = value;
			 if (_backgroundEntity)_backgroundEntity.width = value;
			 updateRender();
		}
		
		
		override public function set height(value:Number):void 
		{
			 if (_borderEntity)_borderEntity.height = value;
			 if (_backgroundEntity)_backgroundEntity.height = value;
			 updateRender();
		}
		
		public function get border():Boolean 
		{
			return _border;
		}
		
		public function set border(value:Boolean):void 
		{
			_border = value;
			if (value) {
				drawBorder();
				_borderEntity.visible = value;
				}else {
				if (_borderEntity) {
					_borderEntity.visible = value;
					}	
				}
		}
		
		/**
		 * 画边框
		 */
		protected function drawBorder():void {
			   if (_borderEntity != null) return;
					_borderEntity  = new Border();
			        _borderEntity.name = "UIComponent_Border";
				    _borderEntity.width = this.width;
					_borderEntity.height = this.height;
					_borderEntity.render(this);
					setChildIndex(_borderEntity, this.numChildren - 1); 
			}
		
		/* ====================================================================================
		 * Methods
		 * ====================================================================================
		 */		 
		 
		 /**
		  * 初始化组件,设置位图数据等
		  */
		protected function initialize():void {
			 
			}
		public function get initialized():Boolean 
		{
			return _initialized;
		}
		
		protected function eventListener():void {
			 
			}
		
		/**
		 * ·是否渲染过
		 */
		public function get IsRender():Boolean 
		{
			return _IsRender;
		}
		
		public function get focus():Boolean 
		{
			return _focus;
		}
		
		public function set focus(value:Boolean):void 
		{
			if (!_focus && value && isActive) {
				   dispatchEvent( new UIComponentEvent(UIComponentEvent.FOCUS_IN));
				}
			if (_focus && !value) {
				  dispatchEvent( new UIComponentEvent(UIComponentEvent.FOCUS_OUT));
				}
			_focus = value;
			
		}
		
		public function get background():Boolean 
		{
			return _background;
		}
		
		public function set background(value:Boolean):void 
		{
			_background = value;
			if (value) {
				drawBackground();
				_backgroundEntity.visible = value;
				}else {
				if (_backgroundEntity) {
					_backgroundEntity.visible = value;
					}	
				}
		}
		
		public function get id():String 
		{
			return _id;
		}
		
		public function set id(value:String):void 
		{
			_id = value;
		}
		
		/**
		 * 画背景
		 */
		protected function drawBackground():void {
			   if (_backgroundEntity == null)_backgroundEntity  = new Background(new BitmapData(1,1,true,0xFFFFFF));
			        _backgroundEntity.name = "UIComponent_Background";
				    _backgroundEntity.width = this.width;
					_backgroundEntity.height = this.height;
					_backgroundEntity.render(this);
					setChildIndex(_backgroundEntity, 0); 
			}
		 
		/**
		 * 更新组件渲染
		 */
		protected function updateRender():void {
			  if (_borderEntity) {
				  drawBorder();
			  }
			  dispatchEvent( new UIComponentEvent(UIComponentEvent.UPDATEVIEW));
			}
			 
		/**
		 * 复位UI
		 */
		public function normal():void {
			 //normal uicomponent.
			  focus = false;
			}
			
		public function dispose():void {
			   if (Disposed) return;
			   
			   if(!_disposed){
		  	   _disposed = true;
			   
			 
			   if (_borderEntity)_borderEntity.dispose();			   
			   if (_backgroundEntity)_backgroundEntity.dispose();			   
			   if (this.parent && !_IsRemoved) this.parent.removeChild(this);
			 
			   }
			}
			
			
		/**
		 * 渲染组件
		 * @param	container
		 */
		public function render(container:DisplayObjectContainer = null):void {

			if(!_IsRender){
			  _IsRender = true;
			  if (container == null) {
				  UIFramework.UiStage.addChild(this);
				  }else{
			      container.addChild(this);
				}
		     }
			 if (_borderEntity) {
				  _borderEntity.width = this.width;
				  _borderEntity.height = this.height;
				 setChildIndex(_borderEntity, this.numChildren - 1); 
			 }
		}
	     
		
				
		private function _addToStageLogic(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, _addToStageLogic);
			if(!_IsRender) dispose();			
		}
		
		private function _removeStageLogic(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _removeStageLogic);
			_IsRemoved = true;
			dispose();
		}
         
		/**
		 * 获得UI Rectangle
		 * @return
		 */
		public function getUIBound():Rectangle {
			return  new Rectangle(this.x, this.y, this.width, this.height);
			}
		 
		/**
		 * 是否和鼠标接触
		 * @return
		 */
		public function hitMouse ():Boolean {
			  if (!visible) return false;
			  var  Gpoint:Point = toGlobalPoint ();
			  var  rect:Rectangle = new Rectangle (Gpoint.x, Gpoint.y, this.width, this.height);
			  return rect.containsPoint(new Point(UIFramework.UiStage.mouseX, UIFramework.UiStage.mouseY));
			}	
			
		/**
		 * 转换成全局坐标
		 * @return
		 */
		public  function toGlobalPoint ():Point {
			  return  UIComponentUtil.toStagePoint (this);
			}
		
		public  function toGlobalBound ():Rectangle {
			 var point:Point = toGlobalPoint();
			  return   new Rectangle(point.x, point.y, width, height);
			}
		public function getBorder():IBorder {
			return _borderEntity;
			}
			
			
		public function getBackground():IBackground {
			return _backgroundEntity;
			}
				
		protected function draw():void {
			  
			}
	}

}